package org.baicaixiaozhan.learn.guava.base;

import com.google.common.base.MoreObjects;

import javax.annotation.Nullable;

/**
 * DESC: {@link MoreObjects#toStringHelper} 示例
 *
 * @author : baicaixiaozhan
 * @since : 2020-11-11
 */
public class ToStringDemo {

    public static void main(String[] args) {

        UserInfo userInfo = new UserInfo(null, 123, "深圳");
        System.out.println(userInfo.toString());
        // UserInfo{userId=123, address=深圳}
    }

    static class UserInfo {
        private String userName;
        private Integer userId;
        private String address;

        public UserInfo() {}
        public UserInfo(@Nullable String userName, Integer userId, String address) {
            this.userName = userName;
            this.userId = userId;
            this.address = address;
        }

        @Override
        public String toString() {
            return MoreObjects.toStringHelper(this)
                    .add("userName", userName)
                    .add("userId", userId)
                    .add("address", address)
                    .omitNullValues()
                    .toString();
        }
    }

}
